﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Resources;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Windows.Threading;

namespace EdBusTracker.V2
{
    public class BusStops
    {
        public Dictionary<String, String> busstops {get; set; }
        public Dictionary<String, String> favstops;
        public String param = null;
        public string stopCode { get; set; }
        private const string URL_PREFIX = "http://mobile.mybustracker.co.uk/mobile.php?searchMode=1&busStopCode=";
        private const string URL_SUFFIX = "&service=&destination=&displayDeparture=Refresh";

        public BusStops()
        {
            loadBusStopsCollection("TextFile1.txt");
        }

        public void loadBusStopsCollection(String filename)
        {
            try
            {
                busstops = new Dictionary<string, string>();
                favstops = new Dictionary<string, string>();
                StreamResourceInfo fs = Application.GetResourceStream(new Uri("TextFile1.txt", UriKind.Relative));
                StreamReader reader = new StreamReader(fs.Stream);
                String line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    String[] aline = line.Split(',');
                    busstops.Add(aline[0], aline[1] + "," + aline[2] + "," + aline[3]);
                }
                reader.Close();
            }
            catch
            { }
        }

        public String getStopURL(String busStopCode)
        {
            String url = URL_PREFIX + busStopCode + URL_SUFFIX;
            return url;
        }


        public List<string> parseBusTimes(String xhtmlString)
        {
            String output = null;
            XDocument xdoc = null;
            List<string> results = new List<string>();
            xdoc = XDocument.Parse(xhtmlString, LoadOptions.None);
            IEnumerable<XElement> rows = xdoc.Root.Elements();
            foreach (XElement row in rows)
            {
                IEnumerable<XElement> items = row.Elements();
                foreach (XElement item in items)
                {
                    if (item.Attribute("colspan") != null)
                        break;
                    output += item.Value + ",";
                }
                results.Add(output);
                output = "";
            }
            return results;
        }

        public String getParam() 
        {
            return param;
        }

        public void setParam(String p){
            param = p;
        }

        public Dictionary<String, String> getBusStops()
        {
            return busstops;
        }

        public String getStopName(String code)
        {
            String[] vals =  busstops[code].Split(',');
            return vals[0];
        }

        public Dictionary<string, string> getFavs()
        {
            return favstops;
        }
    }
}
